﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Facade.Manager.Abstract;

using DataAccess.Repositories;

using Domain.Model;

namespace Facade.Manager
{
    public class PortfolioManager : AbstractManager
    {
        #region Members

        private static readonly PortfolioManager instance = new PortfolioManager();

        private ProgramRepository programRepository;
        private ProjectRepository projectRepository;

        #endregion

        #region Properties

        public List<Program> Programs { get; private set; }

        public List<Project> StandaloneProjects { get; private set; }

        #endregion

        #region PortfolioManager

        private PortfolioManager()
        {
            // instantiate the repositories
            programRepository = new ProgramRepository();
            projectRepository = new ProjectRepository();
        }

        public static PortfolioManager Instance
        {
            get 
            {
                return instance;
            }
        }

        #endregion

        #region public methods

        public List<Program> GetAllPrograms()
        {
            Programs = ProgramManager.Instance.GetAllPrograms();
            return Programs;
        }

        public List<Project> GetAllStandaloneProjects()
        {
            StandaloneProjects = ProjectManager.Instance.GetAllStandaloneProjects();
            return StandaloneProjects;
        }

        public void AddStandAloneProject(Project project)
        {
            if (project != null)
            {
                StandaloneProjects.Add(project);
                projectRepository.Add(project);
            }
        }

        public void AddProgram(Program program)
        {
            if (program != null)
            {
                Programs.Add(program);
                programRepository.Add(program);
            }
        }

        public void UpdateProgram(Program program)
        {
            programRepository.Update(program);
        }

        public void DeleteProgram(int id)
        {
            Program selected = GetProgramById(id);

            if (selected != null)
            {
                programRepository.Delete(selected);
            }
        }

        public Program GetProgramById(int id)
        {
            Program match = null;

            GetAllPrograms();

            foreach (var program in Programs)
            {
                if (program.Id == id)
                {
                    match = program;
                }
            }

            return match;
        }

        #endregion

        #region private methods

        #endregion
    }
}